#include "_collections_deque.h"
#include "pika_hal_ex.h"

void _collections_deque___del__(PikaObj *self){
    pika_hal_deque* que = obj_getPtr(self, "_queue");
    pika_hal_deque_deinit(que);
}
void _collections_deque___init__(PikaObj *self, int maxlen){
    pika_hal_deque* que = pika_hal_deque_create(maxlen);
    if (que == NULL){
        obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
        obj_setSysOut(self, "queue is empty.");
    }
    obj_setPtr(self, "_queue", que);
    obj_setInt(self, "maxlen", maxlen);

}
void _collections_deque_append(PikaObj *self, Arg* item){
    pika_hal_deque* que = obj_getPtr(self, "_queue");

    int result = pika_hal_deque_appendTail(que, (void*)arg_copy(item));
    if (result != 0){
        obj_setErrorCode(self, PIKA_RES_ERR_SIGNAL_EVENT_FULL);
        obj_setSysOut(self, "queue is full.");
    }
}
void _collections_deque_appendleft(PikaObj *self, Arg* item){
    pika_hal_deque* que = obj_getPtr(self, "_queue");

    int result = pika_hal_deque_appendHead(que, (void*)arg_copy(item));
    if (result != 0){
        obj_setErrorCode(self, PIKA_RES_ERR_SIGNAL_EVENT_FULL);
        obj_setSysOut(self, "queue is full.");
    }
}


Arg* _collections_deque_popleft(PikaObj *self){
    pika_hal_deque* que = obj_getPtr(self, "_queue");
    Arg* item;
    int result = pika_hal_deque_popHead(que, (void**)&item);
    if (result == 0){
        return (item);
    }
    else{
        obj_setErrorCode(self, PIKA_RES_ERR_SIGNAL_EVENT_EMPTY);
        obj_setSysOut(self, "queue is empty.");
        return NULL;
    }

}

Arg* _collections_deque_pop(PikaObj *self){
    pika_hal_deque* que = obj_getPtr(self, "_queue");
    Arg* item;
    int result = pika_hal_deque_popTail(que, (void**)&item);
    if (result == 0){
        return (item);
    }
    else{
        obj_setErrorCode(self, PIKA_RES_ERR_SIGNAL_EVENT_EMPTY);
        obj_setSysOut(self, "queue is empty.");
        return NULL;
    }

}

Arg* _collections_deque_fun1(PikaObj *self, Arg* a){
    
    return NULL;
}



int _collections_deque_count(PikaObj *self){
    pika_hal_deque* que = obj_getPtr(self, "_queue");
    return pika_hal_deque_getCount(que);
}
pika_bool _collections_deque_is_empty(PikaObj *self){
    pika_hal_deque* que = obj_getPtr(self, "_queue");
    return pika_hal_deque_isEmpty(que) ? pika_true : pika_false;
}
pika_bool _collections_deque_is_full(PikaObj *self){
    pika_hal_deque* que = obj_getPtr(self, "_queue");
    return pika_hal_deque_isFull(que) ? pika_true : pika_false;
}
